<?php

namespace App\EventSubscriber\Trade;

use App\Base\BaseController;
use App\Consts\Date;
use App\Entity\Trade\UserTrade;
use App\Event\UserEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class UserSubscriber extends BaseController implements EventSubscriberInterface
{

    public function __construct(
    )
    {
        parent::__construct();
    }


    public static function getSubscribedEvents()
    {
        return [
            UserEvent::USER_REG => 'onUserReg',
            UserEvent::USER_DELETE => 'onUserDelete'
        ];
    }
    public function onUserReg(UserEvent $event)
    {
        $user = $event->getUser();
        //获取默认等级
        $u = new UserTrade();
        $u->setUser($user)->setSaleRate(100)->setCreateAt(new \DateTime())->setNote('无');
        $this->myEm()->persist($u);
        $this->myEm()->flush();
    }
    public function onUserDelete(UserEvent $event)
    {
        $user = $event->getUser();
        $this->myEm()->remove($user);
        $this->myEm()->flush();
    }

}
